package cn.imnaer.database.nosql;

import java.util.Properties;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sequoiadb.base.Sequoiadb;
import com.sequoiadb.base.SequoiadbDatasource;
import com.sequoiadb.exception.BaseException;

public class DbConnectionPool {
	private static ApplicationContext applicationContext = null;
	private static SequoiadbDatasource datasource = null;
	static {
		Properties properties = PropertiesUtils.getProperties();
		String config = "ConnectionPool-production.xml";
		applicationContext = new ClassPathXmlApplicationContext(config);
		datasource = (SequoiadbDatasource) applicationContext.getBean("dataSource");
	}
	public static Sequoiadb getConnection() {

		try {
			return datasource.getConnection();
		} catch (BaseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void free(Sequoiadb sdb) {
		if (sdb != null && sdb.isValid())
			datasource.close(sdb);

	}

}
